package models

import (
	"gorm.io/gorm"
	"shop-microservices/basic/inits"
	"time"
)

type ShopCars struct {
	Id         uint32         `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	UserId     uint32         `gorm:"column:user_id;type:int UNSIGNED;comment:用户id;not null;" json:"user_id"`                      // 用户id
	GoodsId    uint32         `gorm:"column:goods_id;type:int UNSIGNED;comment:商品id;not null;" json:"goods_id"`                    // 商品id
	PriceTotal float64        `gorm:"column:price_total;type:decimal(10, 2);comment:总价;not null;default:0.00;" json:"price_total"` // 总价
	Num        uint32         `gorm:"column:num;type:int UNSIGNED;comment:购买数量;not null;" json:"num"`                              // 购买数量
	CreateAt   time.Time      `gorm:"column:create_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_at"`
	UpdateAt   time.Time      `gorm:"column:update_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_at"`
	DeleteAt   gorm.DeletedAt `gorm:"column:delete_at;type:datetime;default:NULL;" json:"delete_at"`
}

func (s *ShopCars) Create() error {
	return inits.DB.Create(&s).Error
}
func (s *ShopCars) UpdateShopCar() error {
	return inits.DB.Where("user_id = ? and goods_id = ?", s.UserId, s.GoodsId).Updates(&s).Error
}
func (s *ShopCars) GetByUserIdAndGoodsId() error {
	return inits.DB.Where("user_id = ? and goods_id = ?", s.UserId, s.GoodsId).Limit(1).Find(&s).Error
}
func (s *ShopCars) DeleteShopCar() error {
	return inits.DB.Where("user_id = ? and goods_id = ?", s.UserId, s.GoodsId).Delete(&s).Error
}
func (s *ShopCars) DeleteShopCarByUserId() error {
	return inits.DB.Where("user_id = ?", s.UserId).Delete(&s).Error
}
